Skip to content

fix(proxy): keep forwarded requests reachable so a client disconnect reaches the daemon - #5058

Merged
thymikee merged 4 commits into
callstack:mainfrom
imranbarbhuiya:fix/proxy-forward-client-disconnect
Nov 24, 2026
Merged

thymikee merged 4 commits into
callstack:mainfrom
imranbarbhuiya:fix/proxy-forward-client-disconnect

Conversation

@imranbarbhuiya

@imranbarbhuiya imranbarbhuiya commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #3256. After a garbage collection, a client leaving a request through agent-device-proxy no longer cancelled it on the daemon.

Undici's Request follows its init signal only while the Request itself is reachable. The proxy kept no reference to the upstream, health and custom transfer requests, or to the one it passes the authenticate hook. After a collection, the client's abort, upstreamTimeoutMs and the health bound no longer reached the transport or the hook. The Node adapter dropped its own request once the response started.

The default fetch loses the abort on Node 24.19 (Undici 7.29.0) but not on Undici 6.21 (the Node 22.12 floor). Custom transports and the hook lose it on both.

Each built request is kept in a WeakMap keyed by the client request, or the adapter's ServerResponse. DaemonProxy.handle and the README ask hosts to keep request reachable until the body ends.

Before / after

pnpm exec vitest run --project unit-core packages/daemon/src/server/http/http-server-proxy.test.ts -t "after a garbage collection":

# base ecb208986, Node 24.19
 FAIL  |unit-core| packages/daemon/src/server/http/http-server-proxy.test.ts > daemon proxy cancels the upstream daemon request when its client disconnects after a garbage collection
Expected: "aborted"
Received: "upstream request was never canceled"
      Tests  1 failed | 12 skipped (13)

# head fae9a52bd, Node 24.19
      Tests  1 passed | 12 skipped (13)

agent-device open through a proxy calling gc() every 500 ms (--expose-gc): at head the daemon cancelled it at 90.13 s; at base it still ran 15 s after the client left.

Validation

Head fae9a52bd.

  • pnpm exec vitest run --project unit-core packages/proxy/src packages/daemon/src/server/http/http-server-proxy.test.ts: 152 passed, three runs; 9 fail at base. Tests force collections via v8.setFlagsFromString('--expose-gc').
  • Mutants: bypassing the keep at any site, or one request per owner, fails 1 to 9 cases.
  • pnpm check:affected --run: format, lint, typecheck, layering, fallow, build and vitest-related passed. pnpm check:daemon-wire-compat: protocol unchanged.

…reaches the daemon

Undici's Request follows its init signal only while the Request itself is
reachable: it links the signal to the Request's own controller through a
weak reference. The proxy handed each Request it built to a transport or
to the authenticate hook and kept no reference, so the first garbage
collection during an exchange cut the link. A client that disconnected
after that, upstreamTimeoutMs and the five-second health bound no longer
reached the transport or the hook, and the daemon kept running the
request.

Each Request the proxy builds is now kept in a WeakMap keyed by the object
it serves: the Node adapter's request by its ServerResponse, the upstream,
health and authenticate requests by the client request, and a custom
transfer transport's request by the upstream one. An owner keeps every
request built for it, so one client request handled twice at once keeps
both. A listener on the followed signal is not used because Undici keeps
that signal reachable for as long as the Request is, so the Request would
never be collected.

The garbage-collection cases move to daemon-proxy-cancellation.test.ts.
@thymikee

Copy link
Copy Markdown
Member

Thanks for the fix. The PR is ready at 8b0d1a8: the change looks correct, and CI is green with no conflicts. Not blocking, and fine to take or leave: the README's "relies on" list in the bring-your-own-transport section could say that the host keeps the Request reachable until the Response body ends, as the DaemonProxy.handle doc now does; one cancellation test could use a real short AbortSignal.timeout after collectGarbage() instead of the mocked signal; and a small forwardedRequest(owner, url, init) helper that builds and keeps the Request in one step would stop the three in-file sites from drifting. Not verified here: the new tests, the base/head comparison, the Node 22.12 / Undici 6.21 behavior, how Bun or Hono hold the Request while sending, and the 90 s live run quoted in the PR body.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 24, 2026
forwardedRequest builds the authenticate, health and upstream requests and keeps them with the client request in one step, so the three sites cannot drift. The Node adapter and custom transfer wrappers keep calling keepAliveWith, since they do not build the request.
The upstreamTimeoutMs case now uses a real 300 ms AbortSignal.timeout after the collection instead of a mocked signal. The mocked health-bound case restores its spy in onTestFinished, so a timeout there cannot leave AbortSignal.timeout mocked for the next case.
@imranbarbhuiya

Copy link
Copy Markdown
Contributor Author
  • Done in fae9a52: the README's bring-your-own-transport list now says the host keeps the Request reachable until the Response body ends, matching DaemonProxy.handle.
  • Done in 84aa855: the upstreamTimeoutMs case ends on a real 300 ms AbortSignal.timeout after collectGarbage(). The mocked health-bound case now restores its spy in onTestFinished, so a timeout there can't leave AbortSignal.timeout mocked for the next case.
  • Done in 5d07b16: forwardedRequest(owner, url, init) builds and keeps the authenticate, health and upstream requests in one step; the adapter and transfer wrappers still call keepAliveWith.

@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 24, 2026
@thymikee

Copy link
Copy Markdown
Member

Thanks for the quick follow-up. The three non-blocking suggestions from the earlier review (#5058 (comment)) are now taken at fae9a52: the README bullet, the real 300 ms timer test, and the shared forwardedRequest helper replacing the three identical call sites. The code looks good and no new problems were found. All 15 checks pass or are skipped at this commit, and the earlier cancelled Body rules run was replaced by a passing run. No conflicts are known, and nothing blocks merge. Not verified here: the tests and the base/head mutants were not run locally, the Node 22.12 / Undici 6.21 behavior and how Bun or Hono hold the Request while sending, and the 90 s live agent-device open run quoted in the PR body. The change is proxy-only, so no device run is needed.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 24, 2026
@thymikee
thymikee merged commit a4934e6 into callstack:main Nov 24, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants